import React, {Component} from 'react';
import {
    StyleSheet,
    ScrollView,
    Text,
    View,
    ImageBackground,
    TouchableOpacity,
    Alert,
    ActivityIndicator,
    FlatList,
    RefreshControl,
} from 'react-native';
import {Button, Flex, WhiteSpace, WingBlank} from '@ant-design/react-native';
import {getParkingFavoriteList} from '~/api/parking';
let pagecount = 0;
let index = 1;
let size = 10;
class Intention extends Component {
    static navigationOptions = {
        title: '意向客户',
    };

    constructor() {
        //调用基类的所有的初始化方法
        super();

        // 设置当前组件的属性
        this.state = {
            list: [],
            page_index: 1,
            page_size: 10,
            isLoading: false, //加载
            refreshing: false,
            footer: 0, //上拉底部显示
        };
        this.getlist();
    }
    getlist() {
        getParkingFavoriteList({
            page_index: this.state.page_index,
            page_size: this.state.page_size,
        }).then((r) => {
            if (r.code === 0) {
                this.setState({
                    list: r.data.list,
                    footer: 0,
                });
                if (this.state.page_index === 1) {
                    pagecount = r.data.total_page_count;
                    this.setState({
                        footer: r.data.total_item_count < 10 ? 2 : 0,
                    });
                }
            } else {
                Alert.alert(r.msg);
            }
        });
    }
    _onRefresh() {
        this.setState({footer: 0, page_index: index, page_size: size}, () => {
            this.getlist();
        });
    }
    //上拉加载更多
    loadData = () => {
        this.setState({isLoading: false});
        let dataArray = [];
        if (pagecount - 1 > 0) {
            pagecount = pagecount - 1;
            this.setState(
                {
                    page_index: this.state.page_index + 1,
                    footer: 1,
                },
                () => {
                    getParkingFavoriteList({
                        page_index: this.state.page_index,
                        page_size: this.state.page_size,
                    }).then((r) => {
                        if (r.code === 0) {
                            dataArray = r.data.list;
                            dataArray = this.state.list.concat(dataArray);
                            this.setState({
                                list: dataArray,
                                isLoading: false,
                            });
                            // Alert.alert('成功');
                        } else {
                            Alert.alert('失败');
                        }
                    });
                },
            );
        } else {
            this.setState({
                footer: 2,
            });
        }
    };

    //上拉底部显示
    genIndicator() {
        if (this.state.footer === 1) {
            return (
                <View style={styles.indicatorContainer}>
                    <ActivityIndicator style={styles.indicator} size={'large'} animating={true} />
                    <Text style={{textAlign: 'center'}}>正在加载更多</Text>
                </View>
            );
        } else if (this.state.footer === 2) {
            return (
                <View
                    style={{
                        height: 30,
                        alignItems: 'center',
                        justifyContent: 'flex-start',
                    }}>
                    <Text
                        style={{
                            color: '#999999',
                            fontSize: 14,
                            marginTop: 5,
                            marginBottom: 5,
                        }}>
                        没有更多数据了
                    </Text>
                </View>
            );
        } else if (this.state.footer === 0) {
            return (
                <View
                    style={{
                        height: 30,
                        alignItems: 'center',
                        justifyContent: 'flex-start',
                    }}>
                    <Text
                        style={{
                            color: '#999999',
                            fontSize: 14,
                            marginTop: 5,
                            marginBottom: 5,
                        }}>
                        上拉加载更多
                    </Text>
                </View>
            );
        }
    }
    _renderItem(item) {
        return (
            <WingBlank>
                <Flex
                    // onPress={() => {
                    //     this.props.navigation.push('IntentionInfo');
                    // }}
                    onPress={() => {
                        this.props.navigation.push('ParkInfo', {
                            parkingid: item.ParkingDataID,
                        });
                    }}
                    direction={'column'}
                    style={{
                        width: '100%',
                        paddingTop: '3%',
                        backgroundColor: 'white',
                        marginTop: 10,
                        borderRadius: 10,
                        paddingBottom: 10,
                    }}>
                    <Flex
                        style={{
                            width: '100%',
                            paddingLeft: 15,
                            paddingRight: 15,
                            paddingTop: 9,
                        }}
                        justify={'between'}>
                        <Text
                            style={{
                                fontSize: 15,
                                color: '#666666',
                            }}>
                            姓名：
                            <Text style={{color: '#333333', fontWeight: 'bold'}}>{item.OwnerName}</Text>
                        </Text>
                        <Text style={{fontSize: 15, color: '#666666'}}>
                            电话：
                            <Text
                                style={{
                                    color: '#333333',
                                    fontWeight: 'bold',
                                }}>
                                {item.Mobile}
                            </Text>
                        </Text>
                    </Flex>
                    <View style={{width: '100%', height: 1, backgroundColor: '#EEEEEE', marginTop: 10}} />
                    <Flex
                        justify={'between'}
                        style={{
                            width: '100%',
                            paddingLeft: 15,
                            paddingRight: 15,
                            paddingTop: 9,
                        }}>
                        <Text
                            style={{
                                fontSize: 16,
                                color: '#666666',
                            }}>
                            <Text
                                style={{
                                    color: '#333333',
                                    fontSize: 16,
                                }}>
                                {item.BuildingName}
                                {item.AreaName}
                                <Text
                                    style={{color: 'blue'}}
                                   >
                                    {item.ParkingName}
                                </Text>
                            </Text>
                        </Text>
                    </Flex>
                    <View style={{width: '100%', height: 1, backgroundColor: '#EEEEEE', marginTop: 10}} />
                    <Flex
                        justify={'between'}
                        style={{
                            width: '100%',
                            paddingLeft: 15,
                            paddingRight: 15,
                            paddingTop: 9,
                        }}>
                        <Text
                            style={{
                                fontSize: 16,
                                color: '#666666',
                            }}>
                            住址:
                            <Text
                                style={{
                                    color: '#333333',
                                    fontSize: 16,
                                }}>
                                {item.CityName}
                                {item.Address}
                            </Text>
                        </Text>
                    </Flex>
                </Flex>
            </WingBlank>
        );
    }
    render() {
        return (
            <FlatList
                data={this.state.list}
                showsVerticalScrollIndicator={false}
                renderItem={(data) => this._renderItem(data.item)}
                keyExtractor={(item, index) => index}
                ListFooterComponent={() => this.genIndicator()} //上拉加载更多视图
                onEndReached={() => {
                    this.loadData();
                }}
                onEndReachedThreshold={0.1} // 这个属性的意思是 当滑动到距离列表内容底部不足 0.1*列表内容高度时触发onEndReached函数 为啥要加这个属性 因为不加的话滑动一点点就会立即触发onReached函数，看不到菊花加载中
                refreshing={true}
                refreshControl={
                    <RefreshControl
                        refreshing={this.state.refreshing}
                        onRefresh={() => {
                            this._onRefresh();
                        }}
                    />
                }
            />
        );
    }
}

const styles = StyleSheet.create({
    wingblank: {
        paddingTop: '2%',
        paddingBottom: '2%',
        borderWidth: 0.3,
        paddingLeft: '1%',
        paddingRight: '1%',
        marginBottom: '3%',
    },
    flextext: {
        paddingBottom: '1%',
    },
});
export default Intention;
